package com.zcmob.web.admanagement.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import brut.androlib.AndrolibException;
import brut.apktool.Main;

public class Unzip {
    public static void main(String[] args) {
        File directory = new File(".");
        // 获取当前工作目录
        String currentPath = null;
        try {
            currentPath = directory.getCanonicalPath();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // String[] apkFilearr = searchApk("d:\\apkssdf", ".apk");
/*        String[] apkFilearr = searchApk(currentPath, ".apk");
        for (String file : apkFilearr) {
            System.out.println(file);
            getIcons(file);
        }*/
        // String f = "d:\\apks\\digua.apk";
        // getIcons(f);
    }

    public static String getIconfile(String targetDir) {
        String manifestFilepath = targetDir + "" + File.separator + "AndroidManifest.xml";
        String keyLine = "android:icon=";
        String iconFilePath = null;
        try {
            File file = new File(manifestFilepath);
            // BufferedInputStream bin = new BufferedInputStream(new
            // FileInputStream(appDatafilePath));
            // byte[] buff = new byte[(int)file.length()];

            InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
            String fileContent = "";
            BufferedReader reader = new BufferedReader(read);
            String lineContent;
            while ((lineContent = reader.readLine()) != null) {
                fileContent = fileContent + lineContent + "\n";
            }
            String[] lines = fileContent.split("\n");
            System.out.println("lines.length   " + lines.length);
            // bin.read(buff);
            OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");

            for (String line : lines) {
                if (line.contains(keyLine)) {
                    System.out.println(line);
                    int fi = line.indexOf("drawable");
                    int end = line.indexOf("\"", fi);
                    while (end < fi) {
                        end = line.indexOf("\"", end);
                    }
                    iconFilePath = "res/drawable/" + line.substring(fi + 9, end) + ".png";
                }
               
            }
            fout.flush();
            fout.close();
            reader.close();
            // bin.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        
        return iconFilePath;
    }

    public static String[] getfilepaths(String dir, String searchname) {
        String[] results = findFileInDir(dir, searchname).toString().split(",");
        return results;
    }

    public static StringBuffer findFileInDir(String dir, String searchname) {
        StringBuffer temp = new StringBuffer();
        File root = new File(dir);
        File[] filesOrDirs = root.listFiles();
        if (filesOrDirs == null)
            return temp;

        String[] result = new String[filesOrDirs.length];
        for (int i = 0; i < filesOrDirs.length; i++) {

            if (filesOrDirs[i].isDirectory()) {
                StringBuffer t = findFileInDir(filesOrDirs[i].getAbsolutePath(), searchname);
                if (t != null && !t.equals(""))
                    temp.append(t);
            } else {
                result[i] = filesOrDirs[i].getName();
                if (result[i].equals(searchname)) {
                    temp.append(filesOrDirs[i]);
                    temp.append(",");
                }
            }
        }
        return temp;
    }

    private static boolean deleteDir(File dir) {

        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so now it can be smoked
        return dir.delete();
    }

    public static String getIcons(final String apkfile) {

        try {
            File apkFile = new File(apkfile);
            String fileName = apkFile.getName();
            String path = apkFile.getParent();
            final String targetDir = path + File.separator + fileName.substring(0, fileName.lastIndexOf(".")) + "Dir";
            final String iconDir = path + File.separator + "iconsDir";
            File dir = new File(targetDir);
            File iconsdir = new File(iconDir);
            if (dir.exists()) {
                deleteDir(dir);
            }
            if (!iconsdir.exists()) {
                iconsdir.mkdir();
            }
            final String[] argss = new String[] { "d", apkfile, targetDir };
            Main.main(argss);
            String result = getIconfile(targetDir);

            return result;
        } catch (AndrolibException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
